from PaSt.past.models import Pandoleros
from PaSt.past.models import Turns
from PaSt.past.models import parseDates

ENEMY_ID = 40

class Team:
    t = []
    
            
    def __init__(self,p=None, selection=False, list=[],daterange="1970-01-01_00:00:00;2020-01-01_00:00:00"):
        
        if(p==None):
            self.t = [turn for turn in Turns.objects.all() if turn.idP != ENEMY_ID]
            
        elif p == 'trening':
            self.t = [turn for turn in Turns.objects.all() if turn.idP != ENEMY_ID and turn.turn_type == 1]

        elif p == 'tekma':
            self.t = [turn for turn in Turns.objects.all() if turn.idP != ENEMY_ID and turn.turn_type == 0]
            
        if(selection):
            self.t = [turn for turn in self.t if turn.idP in list];

	dates=parseDates(daterange)
	self.t = [turn for turn in self.t if turn.date >= dates[0] and turn.date <= dates[1]]

    #######
    #NAPAD#
    #######
    
    def scoreVector(self):
        s =  [turn.final_score for turn in self.t]
        return s
    
    def scoreAverage(self):
        v=self.scoreVector()
	lv = len(v)
	if lv > 0:
	        return float(sum(v))/lv
	else:
		return 0
    
    def scorePositive(self):
        v=self.scoreVector()
        u=[score for score in v if score>0]
	lv = len(v)
	if lv > 0:
	        return float(len(u))/lv 
	else:
		return 0
    
    def scoreChance(self):
        u=[turn for turn in self.t if turn.hit1_id>-1024]
	lt = len(self.t)
	if lt > 0:
	        return float(len(u))/lt
	else:
		return 0
    
    
        
    
    #########
    #OBRAMBA#
    #########
    
    def caughtServCount(self):
        return len([turn for turn in self.t if turn.serv1_id>0 or turn.serv2_id>0 or turn.serv3_id>0])

    def caughtHitCount(self):
        return len([turn for turn in self.t if turn.hit1_id>0 or turn.hit2_id>0 or turn.hit3_id>0])
    
    def baseHitCount(self):
        return len([turn for turn in self.t if turn.base_def_id>0])

    def baseHitServRatio(self):
        sc = len([turn for turn in self.t if (turn.serv1_id==0 or turn.serv2_id==0 or turn.serv3_id==0)])
        hc = len([turn for turn in self.t if turn.base_def_id>0])
	if sc > 0:
	        return float(hc)/sc
	else:
		return 0
    
    def baseDefServRatio(self):
        sc = len([turn for turn in self.t if turn.serv1_id==0 or turn.serv2_id==0 or turn.serv3_id==0])
        hc = len([turn for turn in self.t if turn.base_def_id==0])
        if sc > 0:
	        return float(hc)/sc
	else:
		return 0
    
    
    ######
    #MISC#
    ######
    
    def scoreZeroReason(self):
        #neuspesna serva, izbita baza, ulovljen pandolo, neuspesen udarec
        vector=[0,0,0,0]
        vector[2] = self.caughtServCount() + self.caughtHitCount()
        
        v = [turn for turn in self.t if turn.final_score == 0]
        for turn in v:
            if  (turn.serv1_id<0) and (turn.serv2_id<0) and (turn.serv3_id<0):
                vector[0]+=1
            elif turn.base_def_id>0:
                vector[1]+=1
            elif (turn.hit1_id<0 and turn.hit1_id>-1024) and (turn.hit2_id<0 and turn.hit2_id>-1024) and (turn.hit3_id<0 and turn.hit3_id>-1024):
                vector[3]+=1
        vector[2] = self.caughtServCount() + self.caughtHitCount()
        
        return vector
